import App from './App'

import Vue from 'vue'

// 引入VUEX
import store from './store'
import MinRequest from './utils/MinRequest.js'
// 引入api文件
import minRequest from './api/index.js'
// 检查网络状态
import $U from './utils/checks.js'
import './uni.promisify.adaptor'
import Setting from './setting.js'

// 时间格式化
import dayjs from './libs/dayjs.min.js'
import "./libs/locale/zh-cn";

import weekOfYear from './libs/plugin/weekOfYear/index.js'
import isoWeeksInYear from './libs/plugin/isoWeeksInYear/index.js'
import isLeapYear from './libs/plugin/isLeapYear/index.js'
import dayOfYear from './libs/plugin/dayOfYear/index.js'
import relativeTime from './libs/plugin/relativeTime/index.js'
dayjs.extend(weekOfYear)
dayjs.extend(isoWeeksInYear)
dayjs.extend(isLeapYear)
dayjs.extend(dayOfYear)
dayjs.extend(relativeTime)
dayjs.locale("zh-cn");

import currency from './utils/currency.min.js'
const formatTime = (date, template) => dayjs(date).format(template);
const formatPrice = (price) => currency(price, {
	symbol: '￥'
}).format();
// 时分秒与时间戳相互转化
const timeCycle = (time) => {
	return new Date('1970-01-01' + time).getTime() + 28800000
}

Vue.use(MinRequest)

Vue.prototype.$dayjs = dayjs
Vue.prototype.$U = $U
Vue.prototype.$formatTime = formatTime
Vue.prototype.$currency = currency
Vue.prototype.$formatPrice = formatPrice
Vue.prototype.$timeCycle = timeCycle

/** 
 * 传入xxxx年xx月xx日，返回 template 格式
 */
Vue.prototype.toZhCnDate = (dateString, template) => {
	// 使用正则表达式提取年、月、日
	var [year, month, day] = dateString.match(/\d+/g);
	// 创建一个新的Date对象
	var date = new Date(year, parseInt(month) - 1, day);

	var formattedDate = formatTime(date, template)
	// 输出结果
	return formattedDate
}

let preRouter = ''
let preQuery = {}

/**
 * 路由监听
 */
wx.onAppRoute((res) => {
	const token = store.state.login.token
	if (!Setting.visitor.includes(res.path)) {
		if (!token) {
			uni.navigateTo({
				url: '/pages/login/login'
			})
		}
	}
	if (preRouter === 'pages/detail/detail' && res.path === 'pages/confirm/status/status') {
		if (preQuery.type === 'statusToDetail') {
			uni.navigateBack({delta: 3})
		} else {
			uni.navigateBack({delta: 2})
		}
	} else if ((res.path === 'pages/temporary-form/temporary-form' || res.path === 'pages/file-form/file-form' || res.path === 'pages/confirm/confirm')
	 && preRouter === 'pages/confirm/status/status') {
		if (preQuery.type === 'statusToDetail') {
			uni.navigateBack({delta: 2})
		} else {
			uni.navigateBack({delta: 1})
		}
	}
	preRouter = res.path
	preQuery = res.query
})

Vue.config.productionTip = false
App.mpType = 'app'
const app = new Vue({
	...App,
	store,
	minRequest,
})
app.$mount()